<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>

</head>
<body>
<ul id="ul001">

</ul>
<ul id="ul002">

</ul>
总价格：<input type="text" id="lblall" value="0"/>
<input type="button" value="确认订单" onclick="subOrder()">
<script src="/js/jquery.js"></script>
<script>
    
    function subOrder() {
        $.ajax({
            url: '/addOrder',
            //type: 'post',
            data:{
                carts:JSON.stringify(getQueryString("cartId").split(',')),
                addressId:$("#address").val(),
                coupon:690,
                cashcoupon:1040
            },
            dataType: 'json',
            contentType: 'application/json',
            async: false,
            success: function(data){
                if(data.msgCode="000000")
                {
                    alert("生成成功!");
                }
                else
                {
                    alert("生成失败!");
                }
            }
        });
    }
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    $(document).ready(function () {
        $.ajax({
            url: '/getAddress',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            async: false,
            success: function(data){
                if(data.msgCode="000000")
                {
                    $("#ul001").append("<li id=li"+data.data.id+"><input value="+data.data.id+" type='hidden' id='address'> 姓名："+data.data.name+" 电话："+data.data.phone+" 地址：" +
                        ""+data.data.provinceName+data.data.cityName+data.data.areaName+data.data.detailAddr+"</li>");
                }
                else
                {
                    alert("获取失败!");
                }
            }
        });
        getProduct();
    });

    //可以代金券
    function getMember(levelId,m1,m2,m3,m4,m5) {
//alert(levelId+"---"+m1+"---"+m2+"---"+m3+"---"+m4+"---"+m5);
        var cashCoupon=0;
        switch (levelId)
        {
            case 19:
                cashCoupon=m1;
                return cashCoupon;
            case 16:
                cashCoupon=m2;
                return cashCoupon;
            case 17:
                cashCoupon=m3;
                return cashCoupon;
            case 18:
                cashCoupon=m4;
                return cashCoupon;
            case 20:
                cashCoupon=m5;
                return cashCoupon;
        };
    }
    //节省
    function discount(shoppingId,coupon,cashCoupon,productPrice,productCost,pratorCost,commission,avgCoupon,avgCashCoupon,productCount) {
        var memberSave=0;
        if(avgCoupon==0&&avgCashCoupon!=0)
        {
            memberSave=(parseFloat(productPrice)-parseFloat(productCost)-parseFloat(pratorCost)-parseFloat(cashCoupon))*parseFloat(commission)/100;
        }
        else if(avgCashCoupon==0&&avgCoupon!=0) {
            memberSave=(parseFloat(productPrice)-parseFloat(productCost)-parseFloat(pratorCost)-parseFloat(coupon))*parseFloat(commission)/100;
        }
        else if(avgCashCoupon!=0&&avgCoupon!=0)
        {
            memberSave=(parseFloat(productPrice)-parseFloat(productCost)-parseFloat(pratorCost)-parseFloat(coupon)-parseFloat(cashCoupon))*parseFloat(commission)/100;
        }
        else if(avgCashCoupon==0&&avgCoupon==0)
        {
            memberSave=(parseFloat(productPrice)-parseFloat(productCost)-parseFloat(pratorCost))*parseFloat(commission)/100;
            //alert(parseFloat(productPrice)+"---"+parseFloat(productCost)+"---"+parseFloat(pratorCost)+"---"+parseFloat(commission));
        }
        $("#lblms"+shoppingId).html(memberSave*parseInt(productCount));
        //return memberSave*productCount
    }
    function getProduct() {
        var strs=getQueryString("cartId").split(',');
        $.ajax({
            url: '/getSureCartList',
            data:{
                cartids:JSON.stringify(strs),
            },
            //type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            async: false,
            success: function(data){
                console.log(JSON.stringify(data));
                //alert(data.data.avgCoupon);return;
                if(data.msgCode="000000")
                {
                    var price=0;
                    //alert(data.data.length);
                    $.each(data.data.productInfo, function(i, item){
                        $("#ul002").append("<li id=li"+item.shoppingId+"><img src="+item.imgUrl+">"+item.productName+" "+item.proColor+"--价格：<label>"+item.productPrice+"" +
                            "</label><input onclick='setNumber(this,1,"+item.shoppingId+","+item.coupon+","+item.realCashCoupon+","+item.productPrice+","+item.productCost+","+item.opratorCost+","+item.commission+","+data.data.avgCoupon+","+data.data.avgCashCoupon+","+item.productCount+");' type='button' value='-'/><label id=lbl"+item.shoppingId+">"+item.productCount+"</label>" +
                            "<input onclick='setNumber(this,2,"+item.shoppingId+","+item.coupon+","+item.realCashCoupon+","+item.productPrice+","+item.productCost+","+item.opratorCost+","+item.commission+","+data.data.avgCoupon+","+data.data.avgCashCoupon+","+item.productCount+");'  type='button' value='+'/>" +
                            "可使用兑换券：<label id=lblcou"+item.shoppingId+">"+item.coupon*item.productCount+"</label>" +
                            "可使用折扣券：<label id=lblcash"+item.shoppingId+">"+item.realCashCoupon*item.productCount+"</label>" +
                            "会员节省:<label id=lblms"+item.shoppingId+">"+item.memberSave+"</label>您当前有兑换券:"+data.data.avgCoupon+" 您当前有折扣券:"+data.data.avgCashCoupon+"配送费：</li>");
                        price +=item.productPrice*item.productCount;
                    });
                    $("#lblall").val(price);
                }
                else
                {
                    alert("获取失败!");
                }
            }
        });
    }
    function deleteshoping(shoppingId) {
        $.ajax({
            url: '/deleteCart?shoppingId='+shoppingId,
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            async: false,
            success: function(data){
                if(data.data>0)
                {
                    alert("删除成功!");
                    getPrice();
                    window.location=location;
                }
                else
                {
                    alert("删除失败!");
                }
            }
        });
    }
    function getPrice() {
        var all=0;
        $.each($("#ul002 li"),function (i,item) {
            all+=$.trim($(this).find("label").eq(0).html())*$.trim($(this).find("label").eq(1).html());
        })
        $("#lblall").val(all);
    }
    function setNumber(obj,type,shoppingId,coupon,cashCoupon,productPrice,productCost,pratorCost,commission,avgCoupon,avgCashCoupon,productCount) {
        var oldValue=parseInt($("#lbl"+shoppingId).html());
        if(type==1)
        {
            if(oldValue==1)
            {
                $("#lbl"+shoppingId).html(1);
                getPrice();
            }
            else
            {
                var nowValue=oldValue-1;
                var res=setValue(shoppingId,nowValue);
                if(res>0) {
                    $("#lbl" + shoppingId).html(nowValue);
                    $("#lblcou" + shoppingId).html(coupon*nowValue);
                    $("#lblcash" + shoppingId).html(cashCoupon*nowValue);
                    discount(shoppingId,coupon,cashCoupon,productPrice,productCost,pratorCost,commission,avgCoupon,avgCashCoupon,nowValue);
                    getPrice();
                }
                else
                {
                    alert("添加失败!");
                }
            }
        }
        else if(type==2) {
            var nowValue=oldValue+1;
            var res=setValue(shoppingId,nowValue);
            if(res>0) {
                $("#lbl" + shoppingId).html(nowValue);
                $("#lblcou" + shoppingId).html(coupon*nowValue);
                $("#lblcash" + shoppingId).html(cashCoupon*nowValue);
                discount(shoppingId,coupon,cashCoupon,productPrice,productCost,pratorCost,commission,avgCoupon,avgCashCoupon,nowValue);
                getPrice();
            }
            else
            {
                alert("添加失败!");
            }
        }
    }

    function setValue(shoppingId,value) {
        var res=0;
        $.ajax({
            url: '/updateCart?shoppingId='+shoppingId+"&number="+value,
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            async: false,
            success: function(data){
                if(data.data>0)
                {
                    res=data.data;
                }
            }
        });
        return res;
    }
</script>
</body>
</html>